s = "  hello world  "
s = "the sky is blue"
s = "a good   example"
s = "  hello world  "

var reverseWords = function (s) {
    s = s.trim()
    let result = ''
    let len = s.length
    let left = 0, right = len
    for (let i = len - 1; i >= 0; i--) {
        if (s[i] === ' ' && s[i - 1] !== ' ' && i > 0) {
            left = i
            result += s.slice(left, right).trim() + ' '
            right = left
            console.log(result);

        }
        if (i === 0) {
            result += s.slice(i, right) + ' '
            console.log(result);
        }
    }
    return result.trim()
};
console.log(reverseWords(s));
